import processing.serial.*;

/**
 * oscP5sendreceive by andreas schlegel
 * example shows how to send and receive osc messages.
 * oscP5 website at http://www.sojamo.de/oscP5
 */
 
import oscP5.*;
import netP5.*;
  


OscP5 oscP5;
NetAddress myRemoteLocation;

slider vectorX;
slider vectorY;
slider vectorZ;

boolean bUseArduino=false;

//prefix is the indicator for the sensor number
int prefix1 = 1;
//flag for the end of the signal
boolean myCatch1 = true;
 
 // variables for serial connection, portname and baudrate have to be set 
Serial port;

int[] InputArr= new int [6]; 
int baudrate = 9600;
int val1 = 0;  
String buf1=""; 
int value1=0; 
  



void setup() {
  size(400,400);
  frameRate(25);
  /* start oscP5, listening for incoming messages at port 12000 */
  //we dont really need to listen, so we leave at port 12000
  oscP5 = new OscP5(this,12000);
  
  /* myRemoteLocation is a NetAddress. a NetAddress takes 2 parameters,
   * an ip address and a port number. myRemoteLocation is used as parameter in
   * oscP5.send() when sending osc packets to another computer, device, 
   * application. usage see below. for testing purposes the listening port
   * and the port of the remote location address are the same, hence you will
   * send messages back to this sketch.
   */
  myRemoteLocation = new NetAddress("127.0.0.1",31841);
  
  vectorX=new slider(200,20);
  vectorY=new slider(220,20);
  vectorZ=new slider(240,20);

// if you need to list the available ports, take comment out off
/*
 println(Serial.list());
 
  // establish serial port connection, put the usb number to the [] 
  //port = new Serial(this, Serial.list()[0], baudrate);
  // initialize the input array, which stores the sensor data
  for(int i=0; i < InputArr.length; i++) InputArr[i]=0;
*/
}


void draw() {
  background(128);  
  
  vectorX.draw();
  vectorY.draw();
  vectorZ.draw();
  
  /*
    // listen to serial port and trigger serial event  
  while(port.available() > 0){
    val1 = port.read();
    serialEvent(val1);  
  }
  */

mouseReleased();
}

void serialEvent(int serial){
  if (myCatch1) {
    if(serial!=20) {        
      // add event to buffer
      buf1 += char(serial);          
    } 
    else {
      // if serial is line break set value1 to buf1 and clear it
      value1 = int(buf1);
      InputArr[(prefix1)] = value1;
      myCatch1 = false;     
      buf1="";
    }
  }
  
  //myCatch is the beginging of the each sended number used to define the port
  // myCatch gets true if serial is less than 10 since header is 1-9
  if (!myCatch1 && serial < 20) { 
    myCatch1 = true;
    prefix1 = int(serial);
  }


}

void mouseReleased() {
  /* in the following different ways of creating osc messages are shown by example */
  OscMessage myMessage = new OscMessage("/pilot/vector3f");
  
  //head
  if (bUseArduino){
    myMessage.add(); /* add an int to the osc message */
    myMessage.add(); /* add an int to the osc message */
    myMessage.add(); /* add an int to the osc message */
    }
  else{
    myMessage.add(vectorX.value); /* add an int to the osc message */
    myMessage.add(vectorY.value/10); /* add an int to the osc message */
    myMessage.add(vectorZ.value); /* add an int to the osc message */
    }
  /* send the message */
  oscP5.send(myMessage, myRemoteLocation); 
}


/* incoming osc message are forwarded to the oscEvent method. */
void oscEvent(OscMessage theOscMessage) {
  /* print the address pattern and the typetag of the received OscMessage */
  print("### received an osc message.");
  print(" addrpattern: "+theOscMessage.addrPattern());
  println(" typetag: "+theOscMessage.typetag());
}
